package com.danbay.framework.protocol.command.codec;


import com.danbay.framework.exception.MessageCodeException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * json数据编码
 * @author sjg
 */
public class JsonDataEncoder implements ICmdDataEncoder {
	protected Log logger= LogFactory.getLog(getClass());

	@Override
	public String encode(Object obj) throws MessageCodeException {
		try {
			if(obj==null){
				return null;
			}
			return new ObjectMapper().writeValueAsString(obj);
		} catch (Exception e) {
			throw new MessageCodeException("json编码失败,错误码:1005", 1005, e);
		}
	}

}
